package com.learn.springcloud.eurekahaconsumer.controller;

import com.learn.springcloud.eurekahaconsumer.bean.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * @Time : 2018/7/23 20:02
 * @Version : 1.0
 * @Author : hongyan.li@hand-china.com
 * Copyright @ 2000 Shanghai Hand Co. Ltd.
 * All right reserved.
 */
@RestController
public class PutConsumer {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * put(String url, Object request, Object... uriVariables)
     * @param user
     */
    @PutMapping(value = "/consumer/put/service/example1")
    public String putExample1(@RequestBody Users user){
        System.out.println(user.getName());
        String url = "http://ribbon-resttemplate-example-provider/provider/put/service/example1";
        restTemplate.put(url,user);
        return "success";
    }


    /**
     * put(String url, Object request, Object... uriVariables)
     * @param user
     */
    @PutMapping(value = "/consumer/put/service/example11")
    public String putExample11(@RequestBody Users user){
        System.out.println(user.getName());
        String url = "http://ribbon-resttemplate-example-provider/provider/put/service/example11?anything={anything}";
        restTemplate.put(url,user,"hello world");
        return "success";
    }


    /**
     * put(String url, Object request, Map<String, ?> uriVariables)
     * @param user
     */
    @PutMapping(value = "/consumer/put/service/example2")
    public String putExample2(@RequestBody Users user){
        System.out.println(user.getName());
        String url = "http://ribbon-resttemplate-example-provider/provider/put/service/example1";
        restTemplate.put(url,user,new HashMap<>());
        return "success";
    }


    /**
     * put(String url, Object request, Map<String, ?> uriVariables)
     * @param user
     */
    @PutMapping(value = "/consumer/put/service/example22")
    public String putExample22(@RequestBody Users user){
        System.out.println(user.getName());
        String url = "http://ribbon-resttemplate-example-provider/provider/put/service/example11?anything={anything}";
        Map<String,String> map = new HashMap<>();
        map.put("anything","hello world");
        restTemplate.put(url,user,map);
        return "success";
    }


    /**
     * put(URI url, Object request)
     * @param user
     */
    @PutMapping(value = "/consumer/put/service/example3")
    public String putExample3(@RequestBody Users user){
        System.out.println(user.getName());
        String url = "http://ribbon-resttemplate-example-provider/provider/put/service/example1";
        UriComponents uriComponents = UriComponentsBuilder.fromUriString(url).build().expand("").encode();
        URI uri = uriComponents.toUri();
        restTemplate.put(url,user);
        return "success";
    }


    /**
     * put(URI url, Object request)
     * @param user
     */
    @PutMapping(value = "/consumer/put/service/example33")
    public String putExample33(@RequestBody Users user){
        System.out.println(user.getName());
        String url = "http://ribbon-resttemplate-example-provider/provider/put/service/example11?anything={anything}";
        UriComponents uriComponents = UriComponentsBuilder.fromUriString(url).build().expand("hello world").encode();
        URI uri = uriComponents.toUri();
        restTemplate.put(url,user);
        return "success";
    }
}
